﻿#include "CSensorSeriesRealInfo.h"

CSensorSeriesRealInfo::CSensorSeriesRealInfo(CDevDataSensorSeries* pDevData,QWidget *parent)
	: m_pDevData(pDevData)
{
	ui.setupUi(this);
	m_pRefreshTimer = new QTimer(this);
	setName(m_pDevData->m_sDevName);
	connect(m_pRefreshTimer, SIGNAL(timeout()), this, SLOT(slotRefreshUI()));
}

CSensorSeriesRealInfo::~CSensorSeriesRealInfo()
{
	if (m_pRefreshTimer != nullptr)
	{
		m_pRefreshTimer->stop();
		QEventLoop loop;
		QTimer::singleShot(50, &loop, SLOT(quit()));
		loop.exec();
		delete m_pRefreshTimer;
		m_pRefreshTimer = nullptr;
	}
}

void CSensorSeriesRealInfo::setName(QString sName)
{
	ui.labelName->setText(sName);
}

void CSensorSeriesRealInfo::slotRefreshUI()
{
	int nOkNum = 0;
	int nErrNum = 0;
	m_pDevData->m_pMutex->lock();
	if (m_pDevData->m_nState == 0)
	{
		nErrNum++;
	}
	else if (m_pDevData->m_nState == 1)
	{
		nOkNum++;
	}
	m_pDevData->m_pMutex->unlock();
	ui.labelErrNum->setText(QString::number(nErrNum));
	ui.labelOKNum->setText(QString::number(nOkNum));
}

void CSensorSeriesRealInfo::refreshData(bool bRefresh)
{
	if (bRefresh)
	{
		m_pRefreshTimer->start(3000);
	}
	else
	{
		m_pRefreshTimer->stop();
	}
}
